//
//  YCVideoList.m
//  LOLBox
//
//  Created by mac on 15-3-26.
//  Copyright (c) 2015年 Honey. All rights reserved.
//

#import "YCVideoList.h"
#import "MJRefresh.h"
#import "YCVideoListCell.h"
#import "YCVideoModel.h"
#import "HttpRequestManager.h"
#import "UIBarButtons.h"

#import "menuTip.h"

#import <MediaPlayer/MediaPlayer.h>



@interface YCVideoList ()<UITableViewDataSource,UITableViewDelegate,MJRefreshBaseViewDelegate>
{
    NSInteger _page;    
    // 刷新
    MJRefreshFooterView *_footerView;
    MJRefreshHeaderView *_headerView;
    MJRefreshBaseView *_baseView;
    
    //接收传过来的网址
    NSString *_senderStr;
    
    //弹出菜单
    menuTip *_tip;
    UIButton *_tipBtn;
    
    NSIndexPath *_currentIndexPath;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) NSMutableArray *menuTipArr;

@end

@implementation YCVideoList

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self initMenuTip];
        
        self.menuTipArr = [NSMutableArray array];
        //创建左边返回按钮
        [self createBackBarBtnWithTarget:self WithAction:@selector(popTheView)];
        //导航中间
        [self createNavTitleView:@"视频"];
        
        [self createSearchBarBtn];
    }
    return self;
}
//添加返回事件
- (void)popTheView
{
    if (self.dataSource.count > 0)
    {
        NSLog(@"清数据");
        [self.dataSource removeAllObjects];
    }
    [self.navigationController popViewControllerAnimated:YES];
}


//初始化弹出菜单
- (void)initMenuTip
{
    _tip = [[menuTip alloc] init];
    _tipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //        _tipBtn.frame = CGRectMake(0, CGRectGetHeight(self.view.frame) - 64, self.view.frame.size.width, self.view.frame.size.height - 64 - 50);
    CGRect rect = [[UIScreen mainScreen] bounds];
    _tipBtn.frame = CGRectMake(0, 0, CGRectGetWidth(rect), CGRectGetHeight(rect) - 50);
    [_tipBtn addTarget:self action:@selector(closeMenu:) forControlEvents:UIControlEventTouchUpInside];
    _tipBtn.backgroundColor = [UIColor clearColor];
    _tipBtn.alpha = 1;
    _tip.tag = 100;
    
    [_tip senderIndex:^(NSInteger index) {
        NSLog(@"1111-----%d",index);
        _tip.hidden = YES;
        _tipBtn.hidden = YES;
        if (index == 0 || index == 1 || index == 2)
        {
            NSString *str = self.menuTipArr[index];
            NSLog(@"%@",str);
            
            //加播放器
            [self showVideo:str];
        }
        
    }];
    
    //默认为隐藏
    _tip.hidden = YES;
    _tipBtn.hidden = YES;
    
    //事件
    
    [self.view addSubview:_tipBtn];
    [self.view addSubview:_tip];

}

//播放视频
- (void)showVideo:(NSString *)path
{
    NSString *s = [NSString stringWithFormat:@"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
    NSURL *url = [NSURL URLWithString:s];

    MPMoviePlayerViewController *movie = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    [movie.moviePlayer play];
    
    [self presentMoviePlayerViewControllerAnimated:movie];
}

//点击隐藏菜单和遮挡页面
- (void)closeMenu:(id)sender
{
    [self.tableView deselectRowAtIndexPath:_currentIndexPath animated:NO];
    _tipBtn.hidden = YES;
    _tip.hidden = YES;
}

- (void)viewDidLoad
{
    _page = 1;
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initTableView];
    
    [self initFooterView];
    [self initHeaderView];

    [self.view addSubview:_tipBtn];
    [self.view addSubview:_tip];

}

//传值过来
- (void)senderVaule:(NSString *)str
{
    //传值过来前先将数据源清空
    if (self.dataSource.count > 0)
    {
        [self.dataSource removeAllObjects];
    }
    
    _senderStr = str;
    //NSLog(@"当前的URL ： %@",str);
    [self AFN:_senderStr andPage:_page WithDict:nil];
}


- (void)initTableView
{
    //CGRect rect = [UIScreen mainScreen].bounds;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 50 - 64) style:UITableViewStylePlain];
    //self.tableView.backgroundColor = [UIColor redColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 80;
    //self.tableView.sectionHeaderHeight = 300;
//    self.tableView.backgroundColor = [UIColor redColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    /**
     *  去掉多余的线
     */
    UIView *v = [[UIView alloc] init];
    v.backgroundColor = [UIColor clearColor];
    self.tableView.tableFooterView = v;
    
    // 注册Cell
    [self.tableView registerNib:[UINib nibWithNibName:@"YCVideoListCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:self.tableView];
    
}
//数据请求
- (void)AFN:(NSString *)str andPage:(NSInteger)page WithDict:(NSDictionary *)dict
{
    [[HttpRequestManager shareInstance] getVideoList:str andPage:page withSuccess:^(HttpRequestManager *manager, NSMutableArray *arr) {
        
        _dataSource = arr;
        
        [self.tableView reloadData];
        
        [_baseView endRefreshing];
        
    } andFailure:^(HttpRequestManager *manager, NSError *error) {
        NSLog(@"失败");
        [self.tableView reloadData];
        
        [_baseView endRefreshing];
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCVideoListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (self.dataSource.count > 0)
    {
        YCVideoModel *model = self.dataSource[indexPath.row];
        [cell fillCellWithModel:model];
    }
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

#warning 点击触发 将菜单和页面显示出来
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //记录当前点击的IndexPath
    _currentIndexPath = indexPath;
    
    [self.menuTipArr removeAllObjects];
    //给菜单加事件
    YCVideoModel *m = _dataSource[indexPath.row];
    
    [self.menuTipArr addObject:m.videoAddr];
    [self.menuTipArr addObject:m.videoAddrHigh];
    [self.menuTipArr addObject:m.videoAddrSuper];
    
    //[tableView deselectRowAtIndexPath:indexPath animated:YES];
    //YCVideoModel *model = self.dataSource[indexPath.row];
    CGRect popoverRect = [tableView convertRect:[tableView rectForRowAtIndexPath:indexPath] toView:[tableView superview]];
    //NSLog(@"sss =  %@",NSStringFromCGRect(popoverRect));
    
    CGRect viewRect = CGRectMake(0, self.view.frame.origin.x, self.view.frame.size.width, self.view.frame.size.height);
    [_tip changeMenuTipFrame:popoverRect andViewFrame:viewRect];
    
    _tipBtn.hidden = NO;
    _tip.hidden = NO;
    
}

//判断菜单的高度
- (CGRect)setMenuRect:(CGRect)rect;
{
    CGRect tipRect;
    CGFloat viewHeight = CGRectGetHeight(self.view.frame);
    //NSLog(@"viewHeight:%f",viewHeight);
    
    CGFloat cellHeight = CGRectGetMaxY(rect);
    if (viewHeight - cellHeight - 50 < 80)
    {
        tipRect = CGRectMake(0, CGRectGetMinY(rect) - 80, self.view.frame.size.width, 80);
        return tipRect;
    }
    else
    {
        tipRect = CGRectMake(0, CGRectGetMaxY(rect) , self.view.frame.size.width, 80);
        return tipRect;
    }
}

- (void)dealloc
{
    [_headerView free];
    [_footerView free];
}

- (void)initHeaderView
{
    _headerView = [[MJRefreshHeaderView alloc] initWithScrollView:_tableView];
    _headerView.delegate = self;
}

- (void)initFooterView
{
    _footerView = [[MJRefreshFooterView alloc] initWithScrollView:_tableView];
    _footerView.delegate = self;
}

#pragma mark - MJRefreshBaseViewDelegate

- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    _baseView = refreshView;
    
    if (_baseView == _headerView)
    {
        NSLog(@"刷新");
        
        if (_dataSource.count > 0)
        {
            [_dataSource removeAllObjects];
        }
        _page = 1;
    }
    else
    {
        NSLog(@"加载更多");
        _page++;
    }
    
    NSLog(@"see%d",_page);
    [self AFN:_senderStr andPage:_page WithDict:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
